import { QueryHandler, IQueryHandler, EventBus } from '@nestjs/cqrs';
import { UserExistQuery } from './user-exist.query'
import { Inject } from '@nestjs/common';
import { Repository } from 'typeorm';
import { UserEntity } from '@/user/entities/user.mysql.entity'

@QueryHandler(UserExistQuery)
export class UserExistQueryHandler implements IQueryHandler<UserExistQuery> {
  constructor(
    private readonly eventBus: EventBus,
    @Inject("USER_REPOSITORY")
    private readonly userRepository: Repository<UserEntity>,
  ) { }

  async execute(query: UserExistQuery) {
    console.log(query.params)
    const { username } = query.params
    const result = await this.userRepository.find({ where: { username }, select: ['id'] })
    console.log(result)
    return result.length > 0
  }
}